﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TB_CS
{
    public partial class TB_CS_MainForm : Form
    {
        public TB_CS_MainForm()
        {
            InitializeComponent();
        }
        private static string QR_Scene;
        private void TB_CS_MainForm_Load(object sender, EventArgs e)
        {
            this.Text = "欢迎 " + Class.SysInfo.S_User_Name.ToString() +
                " 登录红包管理系统    --"+Class.SysInfo.INFO_Seller_Nick.ToString();
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            textBox6.Enabled = false;
            textBox7.Enabled = false;
            comboBox2.SelectedIndex = 0;
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                //MessageBox.Show(textBox2.Text.Trim().ToString());
                //MessageBox.Show("Tables:" + Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox2.Text.Trim().ToString()).Tables.Count.ToString());
                //MessageBox.Show("Rows:" + Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox2.Text.Trim().ToString()).Tables[0].Rows.Count.ToString());
                //MessageBox.Show("Tables:" + Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox2.Text.Trim().ToString()).Tables.Count.ToString());

                dataGridView1.DataSource = Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox2.Text.Trim().ToString()).Tables[0];
                textBox2.Text = "";
                textBox2.Focus();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入安全码或用订单号搜索后点击订单号");
                textBox1.Focus();
            }
            else
            {
                DataSet HB_DST = Class.SysInfo.WSHB.GET_QR_INFO(textBox1.Text.Trim().ToString());
                

                if (HB_DST.Tables[0].Rows.Count==0)
                {
                    MessageBox.Show("找不到这个安全码，该安全码红包并未发放，请检查输入是否正确");
                    textBox1.Text = "";
                    textBox1.Focus();
                }
                else
                {
                    string HB_Type = HB_DST.Tables[0].Rows[0][8].ToString();
                    string HB_Money = (Convert.ToInt32(HB_DST.Tables[0].Rows[0][6].ToString()) / 100).ToString();
                    string HB_Create_Time = LeShiTao_Library.Tools.Data_Handle.Convert_UnixTimeStamp_To_Date_Time(HB_DST.Tables[0].Rows[0][5].ToString());
                    string HB_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + HB_DST.Tables[0].Rows[0][3].ToString();
                    QR_Scene = HB_DST.Tables[0].Rows[0][2].ToString();
                    //正常
                    label9.Text = Class.HB_Data_Handle.HB_Type(HB_Type);
                    label10.Text = HB_Money;
                    label11.Text = HB_Create_Time;

                    if (HB_Type == "Wait_Collect" || HB_Type == "UN_Collect"|| HB_Type== "Wait_Collect_Rate_2")
                    {
                        button2.Enabled = true;
                        button3.Enabled = true;
                        button4.Enabled = false;
                    }
                    else if(HB_Type== "Collected")
                    {
                        button2.Enabled = false;
                        button3.Enabled = false;
                        button4.Enabled = true;
                    }
                    else
                    {
                        button2.Enabled = false;
                        button3.Enabled = false;
                        button4.Enabled = false;
                    }
                    pictureBox1.ImageLocation = HB_URL;
                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Plug.Change_Money PCM = new Plug.Change_Money(textBox1.Text.Trim().ToString());
            PCM.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Class.SysInfo.WSHB.END_COLLECT(QR_Scene, Class.SysInfo.S_User_Name.ToString() + "手动作废红包");
            MessageBox.Show("红包作废成功");
            Clear_HB_Form();//清除界面
        }

        private void Clear_HB_Form()
        {
            label9.Text = "";
            label10.Text = "";
            label11.Text = "";
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            textBox1.Text = "";
            pictureBox1.Refresh();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataSet HB_Log_DST = Class.SysInfo.WSHB.GET_HB_LOG_QR_Scene_ID(textBox1.Text.Trim().ToString());
            if(HB_Log_DST.Tables[0].Rows.Count==0)
            {
                MessageBox.Show("该红包暂未有领取记录", "警告");
            }
            else
            {
                string RT_Str = "共检测到成功返现的数据有："+ HB_Log_DST.Tables[0].Rows.Count.ToString()+ "条\n";
                for(int i=0;i< HB_Log_DST.Tables[0].Rows.Count;i++)
                {
                    RT_Str = RT_Str + "发放到微信ID：" + HB_Log_DST.Tables[0].Rows[i][4].ToString() +
                        "  账单编号：" + HB_Log_DST.Tables[0].Rows[i][5].ToString() +
                        "  发放时间：" + LeShiTao_Library.Tools.Data_Handle.Convert_UnixTimeStamp_To_Date_Time(HB_Log_DST.Tables[0].Rows[i][6].ToString())+"\n";
                }
                MessageBox.Show(RT_Str);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Plug.Refresh_HB PRH = new Plug.Refresh_HB(textBox1.Text.Trim().ToString());
            PRH.Show();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("请输入订单号");
                textBox3.Focus();
            }
            else if (comboBox1.Text == "" || comboBox1.Text == "其他直接填写")
            {
                MessageBox.Show("请选择发放原因或者填写发放原因");
            }
            else if (Convert.ToDouble(comboBox2.Text.Trim().ToString()) > 300)
            {
                MessageBox.Show("单次红包大小不得超过人民币：300元，如需发大金额红包可分多笔发送", "警告");
            }
            else if (radioButton1.Checked && Class.SysInfo.WS_TOP.Check_TB_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[0].ToString() == "1")
            {
                MessageBox.Show(Class.SysInfo.WS_TOP.Check_TB_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[1].ToString());
            }
            else if(radioButton4.Checked&&textBox6.Text=="")
            {
                MessageBox.Show("请输入随机起始金额", "Error");
            }
            else if(radioButton4.Checked && textBox7.Text == "")
            {
                MessageBox.Show("请输入随机结束金额", "Error");
            }
            else
            {
                string HB_Type = "";
                string Create_From = "";
                string HB_Insert_Info = "";
                if (radioButton1.Checked)
                {
                    HB_Type = "Wait_Collect";
                }
                else if (radioButton2.Checked)
                {
                    HB_Type = "UN_Collect";
                }
                else if (radioButton3.Checked)
                {
                    HB_Type = "Wait_Collect_Rate_2";
                }
                else if(radioButton4.Checked)
                {
                    HB_Type = "Wait_Collect";
                }
                if (radioButton4.Checked)
                {
                    Random rd = new Random();
                    int rd_db = rd.Next(Convert.ToInt32(textBox6.Text.Trim().ToString())*100,
                        Convert.ToInt32(textBox7.Text.Trim().ToString()) * 100);
                    //随机红包
                    Create_From = Class.SysInfo.S_User_Name + "手动创建随机红包 创建原因：" + comboBox1.Text.Trim().ToString() + "关联用户：" + textBox4.Text.Trim().ToString();
                    HB_Insert_Info = Class.SysInfo.WSHB.INSERT_HB_SERVER(Convert.ToInt32(Class.SysInfo.INFO_Seller_ID.ToString()),
                        Class.SysInfo.WX_User.ToString(), Create_From, textBox3.Text.Trim().ToString(),
                        Convert.ToDouble(rd_db) / 100, HB_Type);
                }
                else
                {
                    Create_From = Class.SysInfo.S_User_Name + "手动创建 创建原因：" + comboBox1.Text.Trim().ToString() + "关联用户：" + textBox4.Text.Trim().ToString();
                    HB_Insert_Info = Class.SysInfo.WSHB.INSERT_HB_SERVER(Convert.ToInt32(Class.SysInfo.INFO_Seller_ID.ToString()),
                        Class.SysInfo.WX_User.ToString(), Create_From, textBox3.Text.Trim().ToString(),
                        Convert.ToDouble(comboBox2.Text.Trim().ToString()), HB_Type);
                }
                if (HB_Insert_Info.Split('@')[0] == "0")
                {
                    //正常
                    string[] HB_Info_List = HB_Insert_Info.Split('@')[1].Split('#');
                    string QR_Scene_ID = HB_Info_List[0].ToString();
                    string QR_Ticket = HB_Info_List[1].ToString();
                    string QR_URL = HB_Info_List[2].ToString();
                    pictureBox2.ImageLocation = @"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + QR_Ticket.ToString();
                    label16.Text = "安全码：" + QR_Scene_ID;
                }
                else
                {
                    MessageBox.Show("创建失败 失败原因：" + HB_Insert_Info.Split('@')[1].ToString());
                }
                //Random rd = new Random();
                //string scene_id;
                //int scene_count;
                //do
                //{
                //    scene_id = rd.Next(1, 2147483646).ToString();
                //    scene_count =Class.SysInfo.WSHB.GET_QR_SCENE_COUNT(scene_id);
                //} while (scene_count != 0);
                //string WX_User=Class.SysInfo.WX_User.ToString();

                //string BR_JSON = LeShiTao_Library.WeChat.Public_WC.BarCode.Create_Temp_BarCode(
                //    Convert.ToInt32(scene_id), Class.SysInfo.WX_User.ToString(), "2592000");

                //string JSON_Ticket = LeShiTao_Library.WeChat.Public_WC.BarCode.GET_Ticket(BR_JSON);
                //string Tids = textBox3.Text.Trim().ToString();
                //int INS_HB = 0;
                //if (radioButton1.Checked)
                //{
                //    INS_HB = Class.SysInfo.WSHB.INSERT_HONGBAO(Convert.ToInt32(Class.SysInfo.INFO_Seller_ID),
                //        scene_id, JSON_Ticket, Tids,
                //        Convert.ToInt32((Convert.ToDouble(comboBox2.Text.Trim().ToString())) * 100),
                //        Class.SysInfo.S_User_Name + "手动创建 创建原因：" + comboBox1.Text.Trim().ToString() + "关联用户：" + textBox4.Text.Trim().ToString());
                //}
                //else if(radioButton2.Checked)
                //{
                //    INS_HB = Class.SysInfo.WSHB.INSERT_HONGBAO_UN(Convert.ToInt32(Class.SysInfo.INFO_Seller_ID),
                //        scene_id, JSON_Ticket, Tids,
                //        Convert.ToInt32((Convert.ToDouble(comboBox2.Text.Trim().ToString())) * 100),
                //        Class.SysInfo.S_User_Name + "手动创建 创建原因：" + comboBox1.Text.Trim().ToString() + "  关联用户：" + textBox4.Text.Trim().ToString());
                //}

                //if (INS_HB == 0)
                //{
                //    MessageBox.Show("二维码生成失败：" + BR_JSON);
                //}
                //else
                //{
                //    label16.Text = "安全码：" + scene_id;
                //    pictureBox2.ImageLocation = @"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + JSON_Ticket;
                //}
            }
        }

        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            //Random rd = new Random();
            //radioButton1.Enabled = false;
            //radioButton2.Checked = true;
            //textBox3.Text = LeShiTao_Library.Tools.Data_Handle.GET_UNIX_TIMESTAMP().ToString() + rd.Next(11111, 99999);

        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            radioButton1.Enabled = true;
            comboBox1.SelectedIndex = 0;
            textBox4.Text = "";
            pictureBox2.Refresh();
            label16.Text = "";
        }

        private void TB_CS_MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
        }

        private void 密码修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ID_Manage.Change_Password IMCP = new ID_Manage.Change_Password();
            IMCP.Show();
        }

        private void 操作日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Log.User_Log LUL = new Log.User_Log();
            LUL.Show();
        }

        private void textBox4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(textBox3.Text=="")
            {
                MessageBox.Show("请输入订单号");
                textBox3.Focus();
            }
            else if(radioButton2.Checked)
            {
                MessageBox.Show("只有好评返现订单才能获取买家ID喔");
            }
            else if (Class.SysInfo.WS_TOP.Check_TB_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[0].ToString() == "1")
            {
                MessageBox.Show(Class.SysInfo.WS_TOP.Check_TB_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[1].ToString());
            }
            else if(Class.SysInfo.WS_TOP.Get_Buyer_Nick_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[0].ToString() == "0")
            {
                textBox4.Text= Class.SysInfo.WS_TOP.Get_Buyer_Nick_Tids(textBox3.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick).Split('#')[1].ToString();
            }
            
        }

        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                if(toolStripTextBox1.Text.Trim()==Class.SysInfo.Info_Seller_PWD.ToString())
                {
                    管理员登录ToolStripMenuItem.Enabled = true;
                    toolStripTextBox1.Text = "";
                }
                else
                {
                    toolStripTextBox1.Text = "";
                    toolStripTextBox1.Focus();
                }
            }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            dataGridView1.DataSource = Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox2.Text.Trim().ToString()).Tables[0];
            textBox2.Text = "";
            textBox2.Focus();
        }

        private void 返现记录查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Log.Admin_Log LAL = new Log.Admin_Log();
            LAL.Show();
        }

        private void 微信菜单管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WeChat_Menu.Menu_Manage WMMM = new WeChat_Menu.Menu_Manage();
            WMMM.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show(Class.SysInfo.WS_TOP.Get_Rate_Feed(textBox5.Text.Trim().ToString(),
                Class.SysInfo.INFO_Seller_Nick.ToString()));
        }

        private void textBox3_Leave(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            { }
            else
            {
                if (radioButton1.Checked)
                {
                    DataSet TID_HB_DST = Class.SysInfo.WSHB.GET_HB_LIST_USE_TID(textBox3.Text.Trim().ToString());
                    if (TID_HB_DST.Tables[0].Rows.Count > 0)
                    {
                        label18.Text = "该订单已有：" + TID_HB_DST.Tables[0].Rows.Count + " 个红包";
                    }
                    else
                    {
                        label18.Text = "";
                    }
                }
                else
                {
                    label18.Text = "";
                }
            }

        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ID_Manage.User_Manage IMUM = new ID_Manage.User_Manage();
            IMUM.Show();
        }

        private void 获取中差评ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Rate.Get_All_Rate RGAR = new Rate.Get_All_Rate();
            RGAR.Show();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            
        }

        private void 中差评查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Rate.Get_Par_Rate RGPR = new Rate.Get_Par_Rate();
            RGPR.Show();
        }

        private void 钉钉配置信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DingTalk.DingTalk_Config DDC = new DingTalk.DingTalk_Config();
            DDC.Show();
        }

        private void 钉钉推送群组设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DingTalk.DingTalk_Robt DTDTR = new DingTalk.DingTalk_Robt();
            DTDTR.Show();
        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {

        }

        private void 地址库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Price_Noti.Link_WH PNLW = new Price_Noti.Link_WH();
            PNLW.Show();
        }

        private void 微信自动回复ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WeChat_Menu.WeChat_Text WMWT = new WeChat_Menu.WeChat_Text();
            WMWT.Show();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton4.Checked)
            {
                comboBox2.Enabled = false;
                textBox6.Enabled = true;
                textBox7.Enabled = true;
            }
            else
            {
                comboBox2.Enabled = true;
                textBox6.Enabled = false;
                textBox7.Enabled = false;
            }
        }

        private void 淘宝授权ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Authorize.TB_Auth ATBA = new Authorize.TB_Auth();
            ATBA.Show();
        }

        private void 物流提醒生成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WX_Express_Noti.Create_Noti WXCN = new WX_Express_Noti.Create_Noti();
            WXCN.Show();
        }

        private void toolStripTextBox1_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (toolStripTextBox1.Text.Trim() == Class.SysInfo.Info_Seller_PWD.ToString())
                {
                    管理员登录ToolStripMenuItem.Enabled = true;
                    toolStripTextBox1.Text = "";
                }
                else
                {
                    toolStripTextBox1.Text = "";
                    toolStripTextBox1.Focus();
                }
            }
        }
    }
}
